<?php

define ('FORMAT_DATE_ISO', 'Y-m-d\TH:i:s');

function controller_base_path($requestContext, $baseUrl = CONTROLLERBASE)
{
	return $baseUrl.'/~'.$requestContext->domain->id;
}

/**
 * returns the url of the profile image
 * @param type $imageSid
 * @param type $width the width of the image
 * @return type 
 */
function profile_img_url($imageSid, $width=NULL, $height=NULL)
{
	$wParam = '';
	if ($width > 0)
		$wParam = '?w='.$width;
	$hParam = '';
	if ($height > 0) {
		$hParam = ( strlen($wParam) > 0 ) ? '&' : '?';
		$hParam .= 'h='.$height;
	}
	if (isset($imageSid)) {
		return RESOURCE_BASE_URL. "/media/image/".$imageSid ."/profile.jpg".$wParam.$hParam; 
	} else {
		// display default image
		return RESOURCE_BASE_URL. "/media/image/-2/profile-default-pic.png/".$wParam.$hParam;
	}

}

function resource_file_url($resource) {
	if ($resource != NULL)
		return RESOURCE_BASE_URL."/resource/file/".$resource->sid."/".$resource->fileName;
	return "";
}

// TODO: migrate to classes/util/Util_datetime.php

/**
 * converts from ISO_8601
 * @param string $datetimeStr
 */
function datetime_parse_from_std($datetimeStr)
{
	return datetime_parse_from($datetimeStr);
}

/**
 * converts from ISO_8601
 * @param string $datetimeStr
 */
function datetime_parse_from($datetimeStr, $format = FORMAT_DATE_ISO)
{
	if ($datetimeStr == null)
		return null;
	$datetimeStr = substr($datetimeStr, 0, 19);
	$retval = DateTime::createFromFormat($format, $datetimeStr);
	
	return $retval;
}

/**
 * Format a date structure to a specific format
 * @param type $datetime
 * @return type 
 */
function datetime_format_std($datetime)
{	
	return datetime_format($datetime);
}

function datetime_format($datetime, $format = FORMAT_DATE_ISO)
{
	if ($datetime == null)
		return "";
	return $datetime->format($format);
}

function datetime_format_convert($datetimeStr, $fromFormat, $toFormat)
{	
	if ($datetimeStr == null)
		return "";
	$datetime = datetime_parse_from($datetimeStr, $fromFormat);
	$retval = $datetime->format($toFormat);
	return $retval;
}

function time_diff($timestamp1, $timestamp2)
{
	$s = $timestamp1-$timestamp2;
	$m=0;$hr=0;$d=0;$td="now"; 
    if($s>59) { 
        $m = (int)($s/60); 
        $s = $s-($m*60); // sec left over 
        $td = "$m min"; 
    } 
    if($m>59){ 
        $hr = (int)($m/60); 
        $m = $m-($hr*60); // min left over 
        $td = "$hr hr"; if($hr>1) $td .= "s"; 
        if($m>0) $td .= ", $m min"; 
    } 
    if($hr>23){ 
        $d = (int)($hr/24); 
        $hr = $hr-($d*24); // hr left over 
        $td = "$d day"; if($d>1) $td .= "s"; 
        if($d<3){ 
            if($hr>0) $td .= ", $hr hr"; if($hr>1) $td .= "s"; 
        } 
    } 
    return $td; 
}

function time_ago($datetimeStr)
{
	$time = datetime_parse_from_std($datetimeStr);
	return time_diff(time(), $time->getTimestamp());
}


/**
 * From a string of format "yyyyMM" where the numbers are optional
 * extract the year and month respectively and returns as array
 * where first element is year and second month
 * @param string $datetimeStr
 */
function parse_yearmonth($str)
{
	if ($str == null || strlen($str) < 4)
		return null;

	$year = intval( substr($str, 0, 4) );
	$month = null;
	if (strlen($str) >= 6)
		$month = intval( substr($str, 4, 2) );
	
	return array($year, $month);
}

/**
 * Given the array returned by parse_yearmonth, format into "Month yyyy"
 * e.g: 201203 yields March 2012
 * @param type $ym
 * @return string 
 */
function format_yearmonth($ym)
{
	if ($ym == null)
		return "";
	
	if (is_string($ym))
		$ym = parse_yearmonth($ym);
	
	$retval = null;
	if ($ym[0] != null)
		$retval = $ym[0];
	if ($ym[1] != null) {
		$strmonth = date("F", mktime(0, 0, 0, ($ym[1] + 1))); 
		$retval = $strmonth.' '.$retval;
	}
	return $retval;
}